package com.maxima.attendance.daos;

import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.maxima.attendance.pojos.Employee;
import com.maxima.attendance.pojos.User;
import com.maxima.attendance.utils.HibernateUtils;

public class UserDAO {
	Map<String, Object> map;

	public User add(User u) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		session.save(u);
		session.getTransaction().commit();
		session.close();
		/*
		 * map.put("usersList", new UserList().initUserList());
		 * this.setSession(map);
		 */
		return u;
	}

	// public void setStatus(Integer id,boolean status) {
	// Session session = HibernateUtils.getSession();
	// session.beginTransaction();
	// User user = null;
	// try {
	// user = (User)session.createQuery("select u from User u where u.id="+
	// id).uniqueResult();
	// user.setLogged_in(status);
	// } catch (HibernateException e) {
	// e.printStackTrace();
	// session.getTransaction().rollback();
	// }
	// session.getTransaction().commit();
	// session.close();
	// }
	public void changePassword(Integer id, String password) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		User user = null;
		try {
			user = (User) session.createQuery("select u from User u where u.id=" + id).uniqueResult();
			user.setPassword(password);
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<User> getAllUsers() {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		List<User> users = null;
		try {
			users = (List<User>) session.createQuery("select u from User u order by u.firstName").list();
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return users;
	}

	@SuppressWarnings("unchecked")
	public List<User> getAllActiveUsers() {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		List<User> users = null;
		try {
			users = (List<User>) session.createQuery("select u from User u where u.active=1 order by u.firstName").list();
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return users;
	}

	@SuppressWarnings("unchecked")
	public List<String> getAllProjectNames() {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		List<String> users = null;
		try {
			users = (List<String>) session.createQuery("select distinct u.project from User u order by u.project").list();
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return users;
	}

	public User getUser(Integer id) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		User user = null;
		try {
			user = (User) session.createQuery("select u from User u where u.id=" + id).uniqueResult();
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return user;
	}

	public Employee getEmployee(Integer id) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		User user = null;
		Employee emp = null;
		try {
			user = (User) session.createQuery("select u from User u where u.id=" + id).uniqueResult();
			emp = new Employee(user, new TimeLogDAO().getLastTimeLog(id));
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
		return emp;
	}

	public void update(Integer id, User u) {
		Session session = HibernateUtils.getSession();
		session.beginTransaction();
		User user = null;
		try {
			user = (User) session.createQuery("select u from User u where u.id=" + id).uniqueResult();
			user.setFirstName(u.getFirstName());
			user.setLastName(u.getLastName());
			user.setEmployeeId(u.getEmployeeId());
			user.setPassword(u.getPassword());
			user.setProject(u.getProject());
			user.setActive(u.getActive());
		}
		catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		session.close();
	}
}
